$(function(){
	//对页面中的元素进行抽取.
	//方法太凌乱,希望统一管理
	//按钮在JS统一进行监听
	var dataDictionaryDatagrid,dataDictionaryEditBtnAndQuitBtn,dataDictionaryDialog,dataDictionaryForm,dataDictionarySearchBtn,
		mainDataDictionaryDatagrid;

	dataDictionaryDatagrid = $("#dataDictionary_datagrid");
	dataDictionaryEditBtnAndQuitBtn = $("#dataDictionary_editBtn,#dataDictionary_quitBtn");
	dataDictionaryDialog = $("#dataDictionary_dialog");
	dataDictionaryForm = $("#dataDictionary_form");
	dataDictionarySearchBtn = $("#searchBtn");
	mainDataDictionaryDatagrid=$("#mainDataDictionary_datagrid");
	//数据表格
	mainDataDictionaryDatagrid.datagrid({
		fit:true,
		rownumbers:true,
		singleSelect:true,
		url:'/dataDictionary/selectAll',
		fitColumns:true,
		columns:[
			[
				{field:'typeName',align:'center',width:10,title:'字典类型'}
			]
		],
		onClickRow:function(index, row){
			var op = dataDictionaryDatagrid.datagrid("options");
			op.url="/dataDictionary/list?type="+row.type;
			dataDictionaryDatagrid.datagrid("load");
		},
		onLoadSuccess:function(data){

			var type = data.rows[0].type;
			var op = dataDictionaryDatagrid.datagrid("options");
			op.url="/dataDictionary/list?type="+type;
			dataDictionaryDatagrid.datagrid("load");
		}
	});

	dataDictionaryDatagrid.datagrid({
		fit:true,
		rownumbers:true,
		singleSelect:true,
		url:'/dataDictionary/list',
		fitColumns:true,
		toolbar:'#dataDictionary_datagrid_tb',
		columns:[
			[
				{field:'name',align:'center',width:10,title:'字典明细名称',formatter:dataFormatter}
			]
		]
	});



	//对话框
	dataDictionaryDialog.dialog({
		width:250,
		height:380,
		buttons:'#dataDictionary_dialog_bt',
		closed:true
	});
    dataDictionarySearchBtn.textbox({
		width:230,
		label:"关键字:",
        labelWidth:50,
        prompt:"请输入搜索关键字",
        buttonText:'搜索',
        buttonIcon:'icon-search',
        onClickButton:function(){
			var keyword = $(this).val();
            dataDictionaryDatagrid.datagrid("load",{
            	keyword:keyword
			});
		}
    });

	//对按钮进行统一事件监听
	$("a[data-cmd]").on("click",function(){
		var cmd = $(this).data("cmd");
		if(cmd){
			cmdObj[cmd]();
		}
	});
	
	//方法统一管理起来]
	var cmdObj = {
			add:function(){
				//1.清空表单数据
				dataDictionaryForm.form("clear");
				//2.设置对话框的标题
				dataDictionaryDialog.dialog("setTitle","新增");
				//3.打开对话框
				dataDictionaryDialog.dialog("open");
			},
			edit:function(){
				var rowData = dataDictionaryDatagrid.datagrid("getSelected");
				if(rowData){
					//1.清空表单数据
					dataDictionaryForm.form("clear");
					//2.设置对话框的标题
					dataDictionaryDialog.dialog("setTitle","新增");
					//3.打开对话框
					dataDictionaryDialog.dialog("open");
					//特殊数据的处理
					if(rowData.dept)
						rowData["dept.id"] = rowData.dept.id;
					//4.回显数据
					dataDictionaryForm.form("load",rowData);//基于同名匹配规则
					//回显角色信息.
					//[1,3]----->List<Long>
					$.post("/role/queryRoleIdListForEmployeeForm?dataDictionaryId="+rowData.id,function(data){
						$("#roleId").combobox("setValues",data);
					}),"json";
					
				}else{
					$.messager.alert("温馨提示","请选择一条需要修改的数据.","warning");
				}
				
			},
			quit:function(){
				var rowData = dataDictionaryDatagrid.datagrid("getSelected");
				if(rowData){
					$.messager.confirm("温馨提示","您确定需要离职该员工吗?",function(yes){
						if(yes){
							$.get("/dataDictionary/quit?id="+rowData.id,function(data){
								if(data.success){
                                    dataDictionaryDatagrid.datagrid("reload");
									$.messager.alert("温馨提示",data.msg,"info");
								}else{
									$.messager.alert("温馨提示",data.msg,"error");
								}
							},"json")
						}
					});
				}else{
					$.messager.alert("温馨提示","请选择需要删除的字典明细记录吗.","warning");
				}
			},
			reload:function(){
				//刷新数据表格
				dataDictionaryDatagrid.datagrid("reload");
			},
			save:function(){
				var url;
				var idVal = $("[name='id']").val();
				if(idVal){
					url = "/dataDictionary/update";
				}else{
					url = "/dataDictionary/save";
				}
				dataDictionaryForm.form("submit",{
					url:url,
					onSubmit:function(param){
						//获取所有的角色信息
						var roleIds = $("#roleId").combobox("getValues");
						//把角色信息放入到表单中
						for(var i=0;i<roleIds.length;i++){
							param["roles["+i+"].id"] = roleIds[i];
						}
						return true;
					},
					success:function(data){
						data = $.parseJSON(data);
						if(data.success){
							//提示消息,当点确定的时候,关闭对话框,刷新数据表格
							$.messager.alert("温馨提示",data.msg,"info",function(){
								dataDictionaryDialog.dialog("close");
								dataDictionaryDatagrid.datagrid("reload");
							});
						}else{
							$.messager.alert("温馨提示",data.msg,"error");
						}
					}
				});
			},
			cancel:function(){
                dataDictionaryDialog.dialog("close");
			}
	}
});
function dataFormatter(value,record,index){
	return value;
}
